//
//  RunAPI.swift
//  MSAPI
//
//  Created by mrsong on 2020/4/15.
//

import Foundation
import MSAPI

public class API {
    public typealias weater = WeatherAPI
}

// MARK: - Thread

var completionInMainThread = true

func wrapInThread<T: Decodable>(_ completion: @escaping APICompletion<T>) -> APICompletion<T> {
    if completionInMainThread {
        return { (response, result) in
            DispatchQueue.main.async {
                completion(response, result)
            }
        }
    } else {
        completionInMainThread = true
        return completion
    }
}

extension API {
    /// Completion in background.
    public static var bg: API.Type {
        completionInMainThread = false
        return self
    }
}
